/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
package edu.fpt.dal;

import edu.fpt.hib.HibernateUtil;
import edu.fpt.hib.Users;
import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Admin
 */
public class UsersDAL {

    public static void insert(Users entity) {
        Session session = null;
        Transaction tran = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            session.save(entity);
            tran.commit();
        } finally {
            session.close();
        }
    }

    public static void update(Users entity) {
        Session session = null;
        Transaction tran = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            session.update(entity);
            tran.commit();
        } finally {
            session.close();
        }
    }

    public static boolean checkLogin(String userID, String password) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            String sql = "from Users where userID = :userid and userpass = :userpass";
            
            Query query = session.createQuery(sql);
            query.setParameter("userid", userID);
            query.setParameter("userpass", password);
            Iterator<Users> it = query.iterate();
            tran.commit();
            while (it.hasNext()) {
                return true;
            }
            return false;
        } finally {
            session.close();
        }
    }

//    public static Users getUsersHQL(String userID) {
//        Session session = null;
//        Transaction tran = null;
//        try {
//            session = (Session) HibernateUtil.getSessionFactory().openSession();
//            tran = session.beginTransaction();
//            Query query = session.createQuery("from Users where userid = '" + userID + "'");
//            Iterator<Users> it = query.iterate();
//            tran.commit();
//            return it.next();
//        } finally {
//            session.close();
//        }
//    }
    public static int getUserRole(String name) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from Users where userid = :userid");
            query.setParameter("userid", name);
            Iterator<Users> it = query.iterate();
            return it.next().getUserRole();
        } finally {
            session.close();
        }
    }

    public static ArrayList<Users> listAll() {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Users> list = new ArrayList<Users>();
            Query query = session.createQuery("from Users");
            list = (ArrayList<Users>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }

    public static ArrayList<Users> searchUsers(String keyword) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Users> list = new ArrayList<Users>();
            Query query = session.createQuery("from Users where userid like '%" + keyword + "%'");
            list = (ArrayList<Users>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }

    public static boolean exist(String userID) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from Users where userId = :userid");
            query.setParameter("userid", userID);
            Users user = (Users) query.uniqueResult();
            tran.commit();
            return user != null;
        } finally {
            session.close();
        }
    }

    /**
     *
     * @param userID
     * @return
     */
    public static Users getUsersHQL(String userID) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from Users where userId = :userid");
            query.setParameter("userid", userID);
            Users user = (Users) query.uniqueResult();
            tran.commit();
            return user;
        } finally {
            session.close();
        }
    }

    public static void delete(String userid) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Users user = (Users) session.get(Users.class, userid);

            session.delete(user);
            tran.commit();
        } finally {
            session.close();
        }
    }
}
